package com.starblink.search.result.ui.cell

import android.graphics.drawable.RotateDrawable
import androidx.core.content.ContextCompat
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.string
import com.starblink.android.basic.graphic.SimpleColorFilter
import com.starblink.basic.style.R
import com.starblink.search.config.SearchConfig
import com.starblink.search.databinding.CellSearchFrontPropertyBinding
import com.starblink.search.result.repository.model.SelectDiscountProperty
import com.starblink.search.result.repository.model.SelectableBrandWrap
import com.starblink.search.result.repository.model.SelectablePropertyWrap

class SearchFrontPropertyCell(
    binding: CellSearchFrontPropertyBinding,
    private val click: ((model: SelectablePropertyWrap, position: Int) -> Unit)? = null,
) :
    BaseBindingVH<SelectablePropertyWrap, CellSearchFrontPropertyBinding>(binding) {

    val clrGray500 = color(R.color.sk_gray500)
    val clrRed600 = color(R.color.sk_gray0)

    init {
        binding.btn.icon = RotateDrawable().apply {
            drawable = ContextCompat.getDrawable(
                itemView.context,
                R.drawable.ic_svg_arrow_down_fill
            )
        }
    }

    override fun onBind(model: SelectablePropertyWrap, position: Int) {
        binding.btn.setOnClickListener {
            click?.invoke(model, position)
        }
        if (model.select) {
            (binding.btn.icon as RotateDrawable?)?.level = 10000 / 2
            //binding.vSelect.visible()
            itemView.isSelected = true
        } else {
            (binding.btn.icon as RotateDrawable?)?.level = 0
            //binding.vSelect.gone()
            itemView.isSelected = false
        }

        val firstSelectProperty = model.properties.firstOrNull { it.selected }
        if (firstSelectProperty != null) {
            binding.btn.text = firstSelectProperty.text
            binding.btn.setTextColor(clrRed600)
            binding.btn.icon.colorFilter = SimpleColorFilter(clrRed600)
//            binding.btn.setStrokeColorResource(R.color.sk_red600)
//            binding.btn.strokeWidth = 1.dp2px()
            binding.btn.setBackgroundColor(color(R.color.gu_bg_light1))
            //binding.vSelect.gone()
            itemView.isSelected = false
        } else {
            binding.btn.text = model.source.name
            binding.btn.setTextColor(clrGray500)
            binding.btn.icon.colorFilter = SimpleColorFilter(clrGray500)
            binding.btn.strokeWidth = 0
            binding.btn.setBackgroundColor(color(R.color.gu_bg_light1))
        }

    }
}

class SearchFrontBrandCell(
    binding: CellSearchFrontPropertyBinding,
    private val click: ((model: SelectableBrandWrap, position: Int) -> Unit)? = null,
) :
    BaseBindingVH<SelectableBrandWrap, CellSearchFrontPropertyBinding>(binding) {

    private val clrGray500 = color(R.color.sk_gray500)
    private val clrRed600 = color(R.color.gu_brand_600)

    init {
        binding.btn.icon = RotateDrawable().apply {
            drawable = ContextCompat.getDrawable(
                itemView.context,
                R.drawable.ic_svg_arrow_down_fill
            )
        }
    }

    override fun onBind(model: SelectableBrandWrap, position: Int) {
        binding.btn.setOnClickListener {
            click?.invoke(model, position)
        }
        if (model.select) {
            (binding.btn.icon as RotateDrawable?)?.level = 10000 / 2
            //binding.vSelect.visible()
            itemView.isSelected = true
        } else {
            (binding.btn.icon as RotateDrawable?)?.level = 0
            //binding.vSelect.gone()
            itemView.isSelected = false
        }

        val firstSelectProperty = model.brands.firstOrNull { it.selected }
        if (firstSelectProperty != null) {
            binding.btn.text = firstSelectProperty.source.name
            binding.btn.setTextColor(clrRed600)
            binding.btn.icon.colorFilter = SimpleColorFilter(clrRed600)
//            binding.btn.setStrokeColorResource(R.color.sk_red600)
//            binding.btn.strokeWidth = 1.dp2px()
            binding.btn.setBackgroundColor(color(R.color.gu_brand_100))
            //binding.vSelect.gone()
            itemView.isSelected = false
        } else {
            binding.btn.text = string(com.starblink.search.R.string.label_brands)
            binding.btn.setTextColor(clrGray500)
            binding.btn.icon.colorFilter = SimpleColorFilter(clrGray500)
            binding.btn.strokeWidth = 0
            binding.btn.setBackgroundColor(color(R.color.gu_bg_light1))
        }

    }
}

class SearchFrontDiscountCell(
    binding: CellSearchFrontPropertyBinding,
    private val click: (() -> Unit)? = null,
) :
    BaseBindingVH<SelectDiscountProperty, CellSearchFrontPropertyBinding>(binding) {

    init {
        binding.btn.icon = RotateDrawable().apply {
            drawable = ContextCompat.getDrawable(
                itemView.context,
                com.starblink.basic.style.R.drawable.icon_search_discount
            )
        }
    }

    override fun onBind(model: SelectDiscountProperty, position: Int) {
        binding.btn.setOnClickListener {
            click?.invoke()
        }

        binding.btn.text = "Discount"
        if (model.sortType == SearchConfig.SORT_DISCOUNT_DOWN) {
            binding.btn.setTextColor(color(R.color.sk_gray0))
            binding.btn.setIconResource(com.starblink.basic.style.R.drawable.icon_search_discount_1)
            binding.btn.setBackgroundColor(color(R.color.gu_bg_dark2))
        } else if (model.sortType == SearchConfig.SORT_DISCOUNT_UP) {
            binding.btn.setTextColor(color(R.color.sk_gray0))
            binding.btn.setIconResource(com.starblink.basic.style.R.drawable.icon_search_discount_2)
            binding.btn.setBackgroundColor(color(R.color.gu_bg_dark2))
        } else if (model.sortType == SearchConfig.SORT_DEFAULT) {
            binding.btn.setTextColor(color(R.color.sk_gray500))
            binding.btn.setIconResource(com.starblink.basic.style.R.drawable.icon_search_discount)
            binding.btn.setBackgroundColor(color(R.color.gu_bg_light1))
            model.sortType = SearchConfig.SORT_DEFAULT
        }

    }
}
